!pip install yfinance
#!pip install pandas
#!pip install requests
!pip install bs4
#!pip install plotly
Requirement already satisfied: yfinance in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (0.2.24) Requirement already satisfied: pandas>=1.3.0 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (2.0.3) Requirement already satisfied: numpy>=1.16.5 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (1.25.1) Requirement already satisfied: requests>=2.26 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (2.30.0) Requirement already satisfied: multitasking>=0.0.7 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (0.0.11) Requirement already satisfied: lxml>=4.9.1 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (4.9.3) Requirement already satisfied: appdirs>=1.4.4 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (1.4.4) Requirement already satisfied: pytz>=2022.5 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (2023.3) Requirement already satisfied: frozendict>=2.3.4 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (2.3.8) Requirement already satisfied: beautifulsoup4>=4.11.1 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (4.12.2) Requirement already satisfied: html5lib>=1.1 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from yfinance) (1.1) Requirement already satisfied: soupsieve>1.2 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.4.1) Requirement already satisfied: six>=1.9 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from html5lib>=1.1->yfinance) (1.16.0) Requirement already satisfied: webencodings in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from html5lib>=1.1->yfinance) (0.5.1) Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from pandas>=1.3.0->yfinance) (2.8.2) Requirement already satisfied: tzdata>=2022.1 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from pandas>=1.3.0->yfinance) (2023.3) Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from requests>=2.26->yfinance) (3.1.0) Requirement already satisfied: idna<4,>=2.5 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from requests>=2.26->yfinance) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from requests>=2.26->yfinance) (2.0.2) Requirement already satisfied: certifi>=2017.4.17 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from requests>=2.26->yfinance) (2023.5.7) Requirement already satisfied: bs4 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (0.0.1) Requirement already satisfied: beautifulsoup4 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from bs4) (4.12.2) Requirement already satisfied: soupsieve>1.2 in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from beautifulsoup4->bs4) (2.4.1)
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
pip install plotly
Collecting plotly
Obtaining dependency information for plotly from https://files.pythonhosted.org/packages/a5/07/5bef9376c975ce23306d9217ab69ca94c07f2a3c90b17c03e3ae4db87170/plotly-5.15.0-py2.py3-none-any.whl.metadata
Downloading plotly-5.15.0-py2.py3-none-any.whl.metadata (7.0 kB)
Collecting tenacity>=6.2.0 (from plotly)
Downloading tenacity-8.2.2-py3-none-any.whl (24 kB)
Requirement already satisfied: packaging in c:\users\jayap\appdata\local\programs\python\python311\lib\site-packages (from plotly) (23.1)
Downloading plotly-5.15.0-py2.py3-none-any.whl (15.5 MB)
---------------------------------------- 0.0/15.5 MB ? eta -:--:--
--------------------------------------- 0.3/15.5 MB 6.3 MB/s eta 0:00:03
--- ------------------------------------ 1.4/15.5 MB 14.4 MB/s eta 0:00:01
----- ---------------------------------- 2.1/15.5 MB 16.5 MB/s eta 0:00:01
------- -------------------------------- 2.9/15.5 MB 17.0 MB/s eta 0:00:01
--------- ------------------------------ 3.8/15.5 MB 17.2 MB/s eta 0:00:01
----------- ---------------------------- 4.6/15.5 MB 17.4 MB/s eta 0:00:01
-------------- ------------------------- 5.5/15.5 MB 17.5 MB/s eta 0:00:01
---------------- ----------------------- 6.4/15.5 MB 17.7 MB/s eta 0:00:01
------------------ --------------------- 7.2/15.5 MB 17.7 MB/s eta 0:00:01
-------------------- ------------------- 8.1/15.5 MB 17.9 MB/s eta 0:00:01
---------------------- ----------------- 8.8/15.5 MB 17.6 MB/s eta 0:00:01
------------------------ --------------- 9.7/15.5 MB 17.7 MB/s eta 0:00:01
--------------------------- ------------ 10.5/15.5 MB 18.7 MB/s eta 0:00:01
----------------------------- ---------- 11.4/15.5 MB 18.7 MB/s eta 0:00:01
------------------------------- -------- 12.3/15.5 MB 18.2 MB/s eta 0:00:01
--------------------------------- ------ 13.1/15.5 MB 18.2 MB/s eta 0:00:01
------------------------------------ --- 14.0/15.5 MB 18.2 MB/s eta 0:00:01
-------------------------------------- - 14.9/15.5 MB 18.7 MB/s eta 0:00:01
--------------------------------------- 15.5/15.5 MB 18.7 MB/s eta 0:00:01
--------------------------------------- 15.5/15.5 MB 18.7 MB/s eta 0:00:01
---------------------------------------- 15.5/15.5 MB 15.6 MB/s eta 0:00:00
Installing collected packages: tenacity, plotly
Successfully installed plotly-5.15.0 tenacity-8.2.2
Note: you may need to restart the kernel to use updated packages.
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data.Date, infer_datetime_format=True), y=stock_data.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data.Date, infer_datetime_format=True), y=revenue_data.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
tesla = yf.Ticker('TSLA')
tesla_data = tesla.history(period="max")
tesla_data.reset_index(inplace=True)
tesla_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 00:00:00-04:00 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0.0 | 0.0 |
| 1 | 2010-06-30 00:00:00-04:00 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0.0 | 0.0 |
| 2 | 2010-07-01 00:00:00-04:00 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0.0 | 0.0 |
| 3 | 2010-07-02 00:00:00-04:00 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0.0 | 0.0 |
| 4 | 2010-07-06 00:00:00-04:00 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0.0 | 0.0 |
url = 'https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue'
html_data = requests.get(url).text
soup = BeautifulSoup(html_data,"html5lib")
tesla_revenue = pd.DataFrame(columns=['Date', 'Revenue'])
for table in soup.find_all('table'):
if ('Tesla Quarterly Revenue' in table.find('th').text):
rows = table.find_all('tr')
for row in rows:
col = row.find_all('td')
if col != []:
date = col[0].text
revenue = col[1].text.replace(',','').replace('$','')
tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
tesla_revenue
| Date | Revenue |
|---|
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'].astype(bool)]
tesla_revenue.tail()
| Date | Revenue |
|---|
gme = yf.Ticker('GME')
gme_data = gme.history(period='max')
gme_data.reset_index(inplace=True)
gme_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 00:00:00-05:00 | 1.620129 | 1.693350 | 1.603296 | 1.691667 | 76216000 | 0.0 | 0.0 |
| 1 | 2002-02-14 00:00:00-05:00 | 1.712708 | 1.716074 | 1.670626 | 1.683251 | 11021600 | 0.0 | 0.0 |
| 2 | 2002-02-15 00:00:00-05:00 | 1.683250 | 1.687458 | 1.658001 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 3 | 2002-02-19 00:00:00-05:00 | 1.666418 | 1.666418 | 1.578047 | 1.607504 | 7410400 | 0.0 | 0.0 |
| 4 | 2002-02-20 00:00:00-05:00 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
url = 'https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'
html_data = requests.get(url).text
soup = BeautifulSoup(html_data,"html5lib")
gme_revenue = pd.DataFrame(columns=['Date', 'Revenue'])
for table in soup.find_all('table'):
if ('GameStop Quarterly Revenue' in table.find('th').text):
rows = table.find_all('tr')
for row in rows:
col = row.find_all('td')
if col != []:
date = col[0].text
revenue = col[1].text.replace(',','').replace('$','')
gme_revenue = gme_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
gme_revenue.tail()
| Date | Revenue |
|---|
make_graph(tesla_data[['Date','Close']], tesla_revenue, 'Tesla')
make_graph(gme_data[['Date','Close']], gme_revenue, 'GameStop')
C:\Users\jayap\AppData\Local\Temp\ipykernel_11512\1276540637.py:3: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument. C:\Users\jayap\AppData\Local\Temp\ipykernel_11512\1276540637.py:4: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.
C:\Users\jayap\AppData\Local\Temp\ipykernel_11512\1276540637.py:3: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument. C:\Users\jayap\AppData\Local\Temp\ipykernel_11512\1276540637.py:4: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.